/*
 * @Author: error: git config user.name & please set dead value or install git
 * @Date: 2022-08-31 10:26:21
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2023-09-19 14:53:11
 * @FilePath: \stm32g431_AllRings\applications\allrings_sys.h
 * @Description:
 *
 * Copyright (c) 2022 by error: git config user.name && git config user.email & please set dead value or install git, All Rights Reserved.
 */
#ifndef __ALLRINGS_SYS_H
#define __ALLRINGS_SYS_H

#include <rtthread.h>
#include <board.h>
#include "rtdebug.h"
#include "my_fdcan_drv.h"
#include "iap_para.h"

// 固件版本
#define ALLRINGS_S_VERSION 5
#define ALLRINGS_S_SUBVERSION 100
#define ALLRINGS_S_REVISION 4
// 硬件版本
#define ALLRINGS_H_VERSION 1
#define ALLRINGS_H_SUBVERSION 0
#define ALLRINGS_H_REVISION 0

/*这几个专门用于判断SRT通讯异常的报错*/
#define DEVICE_RS485_ERR_THRESHOLD 3 // 错误触发阈值
#define DEVICE_RS485_ERR_TIMEOUT 0XFE
#define DEVICE_RS485_ERR_FAILURE 0XFF

typedef enum
{
    RB_DEFAULT,
    RB_DATA_IDLE,
    RB_DATA_BUSY,
} _RBUFF_STA;

enum _RINGS_SYS_CMD
{
    ALLRINGS_SYS_READ_HANDSHAKE = 0x00,  /*系统握手*/
    ALLRINGS_SYS_READ_SWVERSION = 0x01,  /*万能环系统软件版本*/
    ALLRINGS_SYS_READ_ADDRS = 0X02,      /*万能环地址查询*/
    ALLRINGS_SYS_SET_MODE = 0X07,        /*万能环设置系统模式*/
    ALLRINGS_SYS_READ_MODE = 0X55,       /*万能环读系统模式*/
    ALLRINGS_SYS_SET_SAVEPARA = 0X0D,    /*万能环保存系统参数到eeprom*/
    ALLRINGS_SYS_SET_SWITCH = 0x2A,      /*万能环开关设置*/
    ALLRINGS_SYS_READ_SWITCH = 0x2B,     /*万能环开关查询*/
    ALLRINGS_SYS_SET_RES485 = 0x2C,      /*万能环RS485匹配电阻使能*/
    ALLRINGS_SYS_SET_CANID = 0x3D,       /*设置canid*/
    ALLRINGS_SYS_SET_FDCANINDEX = 0XCD,  /*设置FDCAN广播命令-索引号*/
    ALLRINGS_SYS_READ_FDCANINDEX = 0XCE, /*读取FDCAN广播命令-索引号*/
    ALLRINGS_SYS_READ_HWVERSION = 0x96,  /*万能环系统硬件版本*/
    ALLRINGS_SYS_SET_IAP_FLAG = 0x97,    /*设置IAP状态标志*/
    AIIRINGS_SYS_READ_DEVICE = 0XFA,     /*读取万能环设备对象*/
    AIIRINGS_SYS_SET_DEVICE = 0XFB,      /*设置万能环设备对象*/
    ALLRINGS_SYS_SET_CLEAR = 0xFE,       /*清除系统错误*/
    ALLRINGS_SYS_READ_ERR = 0xFF,        /*查询系统错误*/

    ALLRINGS_SYS_SET_CAN_RES = 0XD0,  /*设置can匹配电阻*/
    ALLRINGS_SYS_READ_CAN_RES = 0XD1, /*查询can匹配电阻*/
    ALLRINGS_SYS_SET_485_RES = 0XD2,  /*设置485匹配电阻*/
    ALLRINGS_SYS_READ_485_RES = 0XD3, /*查询485匹配电阻*/

};

typedef union
{
    uint32_t Byt;
    struct _B_
    {
        unsigned short int L;
        unsigned short int H;
    } B;
    struct bit_s
    {
        // 低16bit
        unsigned int b0 : 1; //
        unsigned int b1 : 1; //
        unsigned int b2 : 1; //
        unsigned int b3 : 1; //
        unsigned int b4 : 1; //
        unsigned int b5 : 1; //
        unsigned int b6 : 1; //
        unsigned int b7 : 1; //

        unsigned int IAP : 1; // IAP错误
        unsigned int b9 : 1;
        unsigned int b10 : 1;
        unsigned int b11 : 1;
        unsigned int b12 : 1;
        unsigned int b13 : 1;
        unsigned int b14 : 1;
        unsigned int b15 : 1;

        // 高16bit

        unsigned int EPG_ERR_NO_ACTIVE : 1; //
        unsigned int EPG_ERR_CTR_CMD : 1;   //
        unsigned int EPG_ERR_NO_COMM : 1;   //
        unsigned int EPG_ERR_CUR : 1;       //
        unsigned int EPG_ERR_VOL : 1;       //
        unsigned int EPG_ERR_ENABLE : 1;    //
        unsigned int EPG_ERR_OVER_TEMP : 1; //
        unsigned int EPG_ERR_ITSELF : 1;    //

        unsigned int CAN_ERR : 1;       // CAN通讯错误
        unsigned int RS485_ERR : 1;     //
        unsigned int RS485_TIMEOUT : 1; //
        unsigned int EEPROM : 1;        //  EEPROM错误
        unsigned int Tmp_Err : 1;       //
        unsigned int SRT_GAS : 1;       // SRT漏气
        unsigned int BUS_V_L : 1;       //
        unsigned int NO_DEVICE : 1;     // 无名设备

    } bit;

} _ALLRINGS_SYS_ERR;

/*万能环系统参数*/
struct _allrings_sys_values
{
    _RBUFF_STA analaysis_data_type; // 缓存状态：0->省缺数据；1->空闲；2->忙
    uint8_t all_switch;             // 万能环使能开关
    uint8_t can_res_sta;            // CAN匹配电阻状态
    uint8_t rs485_res_sta;          // 485匹配电阻状态
    uint8_t sys_mode;               // 系统模式
    _ALLRINGS_SYS_ERR allsys_err;   // 万能环系统错误
};
struct _ALLRINGS_CTR
{
    struct _allrings_sys_values allrings_values;
    struct _srt_ctr *_srt_ctr;   // 气动收结构体指针
    struct _epg_ctr *_epg_ctr_p; // 电动手结构体指针

    // void (*allrings_sys_ctr[256])(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
    void (*can_sysback_data)(struct _fdcan_rxdata *fdcandata);
};
typedef struct _ALLRINGS_CTR *_ALLRINGS_CTR_T;

extern struct _ALLRINGS_CTR rings_ctr;

static void fdcan_sysback_data(struct _fdcan_rxdata *fdcandata);

// void allrings_sys_init(struct _ALLRINGS_CTR *ALL);
void upper_candata2modbus_analysis(struct _ALLRINGS_CTR *allringctr, struct _fdcan_rxdata *fd_data);
void allrings_device_ctr(struct _ALLRINGS_CTR *allringctr, union union_sys_savepara *sys_para, struct _fdcan_rxdata *fd_data);
/*****************************************************/
void allrings_sys_r_handshake(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_r_swversion(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_r_addrs(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_r_mode(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_w_mode(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_r_hwversion(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_w_savedpara(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_r_switch(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_w_switch(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_w_device(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_w_res_rs485(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_r_addr(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_r_canid(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_w_canid(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_r_fdcanindex(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_w_fdcanindex(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_w_iap(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_w_clear(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_r_err(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);

void allrings_sys_w_canres(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_r_canres(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_w_485res(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);
void allrings_sys_r_485res(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);

void allrings_sys_r_device(struct _ALLRINGS_CTR *allctr, struct _fdcan_rxdata *fd_data);

#endif
